package max.test;

public class Sort {

	public static void mergeSort(int[] array, int l, int r) {
		if (l == r)
			return;
		int m = (l + r) / 2;
		Sort.mergeSort(array, l, m);
		Sort.mergeSort(array, m + 1, r);
		Sort.merge(array, l, r);
	}

	private static void merge(int[] array, int l, int r) {
		if (l == r)
			return;
		int m = (l + r) / 2;
		int[] temp = new int[r - l + 1];// 小的-大的error
		int llp = l;
		int rlp = m + 1;
		int pp = 0;
		while (llp <= m && rlp <= r) {
			temp[pp++] = array[llp] < array[rlp] ? array[llp++] : array[rlp++];
		}
		while (llp <= m) {// 循环写成if error
			temp[pp++] = array[llp++];
		}
		while (rlp <= r) {
			temp[pp++] = array[rlp++];
		}
		for (int element : temp) {
			array[l++] = element;
		}
	}

	public static void main(String[] args) {
		int[] array = new int[] { 5, 2, 1, 9, 10, 4, 6, 7 };
		mergeSort(array, 0, array.length - 1);
		for (int a : array) {
			System.out.println(a);
		}
	}

}
